﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Order.Entity;
using Runda.CMS.Order.Repository;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.OrderManagement
{
    public partial class ShippingReceive : BasePage
    {
        private ShippingRequestRepository shippingRequestRepository = new ShippingRequestRepository();

        private ShippingRepository shippingRepository = new ShippingRepository();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["sendCompany"] != String.Empty && Request.QueryString["shippingNum"] != String.Empty)
                {
                    List<Shipping> shippings =  shippingRepository.findShippingByBillNumAndSendCompany(Request.QueryString["shippingNum"], Request.QueryString["sendCompany"]);
                    RepeaterShippings.DataSource = shippingRepository.findShippingByBillNumAndSendCompanyG(Request.QueryString["shippingNum"], Request.QueryString["sendCompany"]); ;
                    RepeaterShippings.DataBind();
                    txtPreReceivedDate.SelectedDate = shippings[0].preReceivedDate;
                    txtSendBillNo.Text = shippings[0].sendBillNo;
                    txtSendCompany.Text = shippings[0].sendCompany;
                    if (shippings[0].receivedDate != null && shippings[0].receivedMan != String.Empty) {
                        lnkbtnSave.Visible = false;
                        lnkbtnSaveAndClose.Visible = false;
                    }
                }
            }
        }

        protected void RepeaterShippings_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
                      
        }

        protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "alert('保存成功');", true);
        }

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            Save();
            ClientScript.RegisterStartupScript(this.GetType(), "saveandclose", "window.close();", true);
        }

        private void Save()
        {
            List<Shipping> shippings = shippingRepository.findShippingByBillNumAndSendCompany(Request.QueryString["shippingNum"], Request.QueryString["sendCompany"]);
            using(System.Transactions.TransactionScope trans = new System.Transactions.TransactionScope()){
                foreach(Shipping shipping in shippings){
                    shipping.receivedDate = radReceivedDate.SelectedDate.Value;
                    shipping.receivedMan = txtReceivedMan.Text.Trim();
                    shipping.updatedBy = base.CurrentUserID;
                    shipping.updatedOn = DateTime.Now;
                    shippingRepository.merge(shipping);
                }
                trans.Complete();
            }
        }
    }
}
